package com.hinnotek.amoeba.ui.activity.old

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.lee.common.base.BaseActivity
import com.hinnotek.amoeba.R
import com.hinnotek.amoeba.adapters.old.ProductManagementAdapter
import com.hinnotek.amoeba.bean.old.ProductManagamentBean
import com.hinnotek.amoeba.databinding.IncludeRecyclerviewBinding
import com.hinnotek.amoeba.dialog.CustomDialog
import com.hinnotek.amoeba.ext.init
import com.hinnotek.amoeba.utilities.IntentUtils
import com.hinnotek.amoeba.utilities.StatusBarUtil
import com.hinnotek.amoeba.utilities.ToastUtils
import com.hinnotek.amoeba.viewmodels.PublicInterfaceViewModel
import com.lee.common.network.net.ObserverState
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class ProductManagamentActivity : BaseActivity<IncludeRecyclerviewBinding>() {

    private var mAdapter: ProductManagementAdapter? = null
    private var customDialog: CustomDialog? = null


    private val viewModel by viewModels<PublicInterfaceViewModel>()

    private var mytitle: String? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mytitle = intent.getStringExtra("title")
        setLeftVisible(true)
        setTitle(mytitle)

        setContentView(R.layout.include_recyclerview)
    }

    override fun initVew() {
        StatusBarUtil.setStatusBarColor(this, R.color.colorbtns)
        bindingView.recyclerView.init()
        bindingView.recyclerView.adapter = ProductManagementAdapter().also { mAdapter = it }
        mAdapter?.setOnItemClickListener(OnItemClickListener { adapter: BaseQuickAdapter<*, *>, view: View?, position: Int ->
            val productManagament = adapter.getItem(position) as ProductManagamentBean
            val bundle = Bundle()
            bundle.putInt("productTypeId", productManagament.productTypeId)
            bundle.putString("title", mytitle)
            bundle.putString("productTypeName", productManagament.productTypeName)
            bundle.putString("productPrice", productManagament.productPrice)
            IntentUtils.startActivityForResult(
                this@ProductManagamentActivity,
                ProductManagermentAddActivity::class.java,
                bundle,
                1
            )
        })
    }

    override fun setData() {
        customDialog = CustomDialog(mContext, "正在加载...")
        customDialog?.show()
        getProductManagamentList()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (requestCode == 1) {
            customDialog = CustomDialog(mContext, "正在加载...")
            customDialog?.show()
            getProductManagamentList()
        }
        super.onActivityResult(requestCode, resultCode, data)
    }

    /**
     * app端获取某个产品下的所有型号接口
     */
    private fun getProductManagamentList(){
        val productId = intent.getIntExtra("productId", 0)
        viewModel.getProductTypeListByProductId(productId).observe(this,object : ObserverState<MutableList<ProductManagamentBean>>(){
            override fun onDataChange(data: MutableList<ProductManagamentBean>) {
                customDialog?.dismiss()
                mAdapter?.setList(data)
                super.onDataChange(data)
            }

            override fun onError(msg: String?, code: Int) {
                ToastUtils.showShort(mContext, msg)
                customDialog?.dismiss()
                super.onError(msg, code)
            }
        })

    }
}